package com.wwwarehouse.resource_center.fragment.createobject.creategoods;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;

import com.wwwarehouse.common.adapter.carddesk.MyFragmentPagerAdapter;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.CategoryBean;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;


/**
 * Description:  添加类目
 * Copyright  : Copyright (c) 2017
 * Author     : yfwang
 * Date       : 2017/9/7 14:28
 */

public class AddCategoryParentFagment extends TheParentFragment {

    CustomViewPagerInternal viewPager;
    CirclePageIndicator pageIndicator;
    ArrayList<Fragment> fragmentList;
    ElasticScrollView elasticScrollView;
    MyFragmentPagerAdapter adapter;
    private Button mConfirm;
    int mNum = 20;

    @Override
    public void setTitle() {
        if (peekFragment() instanceof AddCategoryParentFagment)
            mActivity.setTitle(R.string.select);
    }

    @Override
    protected int getContentId() {
        return R.layout.fragment_goods_category_parent;
    }

    @Override
    protected void init(View view) {
        elasticScrollView = findView(view, R.id.elastic_scroll);
        viewPager = findView(view, R.id.task_pager);
        pageIndicator = findView(view, R.id.pager_indicator);
        mConfirm = findView(view, R.id.confirm);
        EventBus.getDefault().register(this);


        Bundle bundle = getArguments();
        if (bundle == null)
            return;

        List<CategoryBean.EmptyIdentifierBean> caregotyBeen = bundle.getParcelableArrayList("addData");

        fragmentList = new ArrayList<>();
        int count = 0;
        if (caregotyBeen.size() > mNum) {
            if ((caregotyBeen.size() - mNum) % mNum == 0) {
                count = (caregotyBeen.size() - mNum) / mNum;
            } else {
                count = (caregotyBeen.size() - mNum) / mNum + 1; // 总共显示多少张卡片
            }
        } else {
            count = 0;
        }

        for (int i = 0; i <= count; i++) {
            AddCategotyFragment categoryFragment = new AddCategotyFragment();
            Bundle b = new Bundle();
            b.putParcelableArrayList("data", (ArrayList<? extends Parcelable>) caregotyBeen);
            b.putInt("pos", i);
            b.putInt("num", mNum);
            categoryFragment.setArguments(b);
            fragmentList.add(categoryFragment);

        }
        adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentList);
        viewPager.setAdapter(adapter);
        pageIndicator.setViewPager(viewPager, 0);

    }

    public void onEventMainThread(Object event) {
    }


    @Override
    protected void loadDatas() {

    }


    @Override
    public void onSuccess(CommonClass commonClass, int what) {

    }

    @Override
    public void onReLoad(int code) {
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
